﻿using LCL.CodeHelper;
using LCL.IBuilder;
using LCL.LData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LCL.Builder.SimpleFactory
{
    public partial class ProjectBuilder
    {
        #region BuilderDALFactory
        private void BuilderDALFactory()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("using System; ");
            builder.AppendLine("using System.Reflection; ");
            builder.AppendLine("using System.Configuration; ");
            builder.AppendLine("using LCL.Caching; ");
            builder.AppendLine("namespace " + NameSpace + ".DALFactory ");
            builder.AppendLine("{ ");
            builder.AppendLine("	/// <summary> ");
            builder.AppendLine("    /// 请检查web.config里是否修改了<add key=\"DAL\" value=\"LCL.SQLServerDAL\" />。 ");
            builder.AppendLine("	/// </summary> ");
            builder.AppendLine("	public sealed class DataAccess  ");
            builder.AppendLine("	{ ");
            builder.AppendLine("        private static readonly string AssemblyPath = ConfigurationManager.AppSettings[\"DAL\"];         ");
            builder.AppendLine("        private static LCache cache = LCache.Instance;          ");
            builder.AppendLine("		public DataAccess() ");
            builder.AppendLine("		{ } ");
            builder.AppendLine("        #region CreateObject  ");
            builder.AppendLine("		//不使用缓存 ");
            builder.AppendLine("        private static object CreateObjectNoCache(string AssemblyPath,string classNamespace) ");
            builder.AppendLine("		{		 ");
            builder.AppendLine("			try ");
            builder.AppendLine("			{ ");
            builder.AppendLine("				object objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);	 ");
            builder.AppendLine("				return objType; ");
            builder.AppendLine("			} ");
            builder.AppendLine("			catch ");
            builder.AppendLine("			{ ");
            builder.AppendLine("				return null; ");
            builder.AppendLine("			}			 ");
            builder.AppendLine("        } ");
            builder.AppendLine("		//使用缓存 ");
            builder.AppendLine("		private static object CreateObject(string AssemblyPath,string classNamespace) ");
            builder.AppendLine("		{			 ");
            builder.AppendLine("			object objType = cache.Get<object>(classNamespace); ");
            builder.AppendLine("			if (objType == null) ");
            builder.AppendLine("			{ ");
            builder.AppendLine("				try ");
            builder.AppendLine("				{ ");
            builder.AppendLine("					objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);					 ");
            builder.AppendLine("					cache.Set(classNamespace, objType);// 写入缓存 ");
            builder.AppendLine("				} ");
            builder.AppendLine("				catch ");
            builder.AppendLine("				{ ");
            builder.AppendLine("				} ");
            builder.AppendLine("			} ");
            builder.AppendLine("			return objType; ");
            builder.AppendLine("		} ");
            builder.AppendLine("        #endregion ");
            var tables = DbObject.GetTables(DbName);
            foreach (var table in tables)
            {
                string className = CodeCommon.GetClassName(table.TableName, TableFormat);

                builder.AppendLine("        #region Create" + className + " ");
                builder.AppendLine("        public static " + NameSpace + ".IDAL.I" + className + "DAL Create" + className + "() ");
                builder.AppendLine("		{		 ");
                builder.AppendLine("			string classNamespace = AssemblyPath+\"." + className + "\";	 ");
                builder.AppendLine("			object objType=CreateObject(AssemblyPath,classNamespace); ");
                builder.AppendLine("            return (" + NameSpace + ".IDAL.I" + className + "DAL)objType;		 ");
                builder.AppendLine("		} ");
                builder.AppendLine("		#endregion ");

            }
            builder.AppendLine("	} ");
            builder.AppendLine("} ");

            string filename = CodeCommon.DesktopDirectory + NameSpace + @"/DALFactory/DataAccess.cs";
            CodeCommon.CreateFiles(filename, builder.ToString());
            DALFactoryCsproj();
        }
        private void DALFactoryCsproj()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?> ");
            builder.AppendLine("<Project DefaultTargets=\"Build\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\" ToolsVersion=\"4.0\"> ");
            builder.AppendLine("  <PropertyGroup> ");
            builder.AppendLine("    <ProjectType>Local</ProjectType> ");
            builder.AppendLine("    <ProductVersion>8.0.50727</ProductVersion> ");
            builder.AppendLine("    <SchemaVersion>2.0</SchemaVersion> ");
            builder.AppendLine("    <ProjectGuid>{" + DALFactoryGuid + "}</ProjectGuid> ");
            builder.AppendLine("    <Configuration Condition=\" '$(Configuration)' == '' \">Debug</Configuration> ");
            builder.AppendLine("    <Platform Condition=\" '$(Platform)' == '' \">AnyCPU</Platform> ");
            builder.AppendLine("    <ApplicationIcon> ");
            builder.AppendLine("    </ApplicationIcon> ");
            builder.AppendLine("    <AssemblyKeyContainerName> ");
            builder.AppendLine("    </AssemblyKeyContainerName> ");
            builder.AppendLine("    <AssemblyName>" + NameSpace + ".DALFactory</AssemblyName> ");
            builder.AppendLine("    <AssemblyOriginatorKeyFile> ");
            builder.AppendLine("    </AssemblyOriginatorKeyFile> ");
            builder.AppendLine("    <DefaultClientScript>JScript</DefaultClientScript> ");
            builder.AppendLine("    <DefaultHTMLPageLayout>Grid</DefaultHTMLPageLayout> ");
            builder.AppendLine("    <DefaultTargetSchema>IE50</DefaultTargetSchema> ");
            builder.AppendLine("    <DelaySign>false</DelaySign> ");
            builder.AppendLine("    <OutputType>Library</OutputType> ");
            builder.AppendLine("    <RootNamespace>" + NameSpace + ".DALFactory</RootNamespace> ");
            builder.AppendLine("    <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent> ");
            builder.AppendLine("    <StartupObject> ");
            builder.AppendLine("    </StartupObject> ");
            builder.AppendLine("    <FileUpgradeFlags> ");
            builder.AppendLine("    </FileUpgradeFlags> ");
            builder.AppendLine("    <UpgradeBackupLocation> ");
            builder.AppendLine("    </UpgradeBackupLocation> ");
            builder.AppendLine("    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> ");
            builder.AppendLine("    <OldToolsVersion>2.0</OldToolsVersion> ");
            builder.AppendLine("    <PublishUrl>publish\\</PublishUrl> ");
            builder.AppendLine("    <Install>true</Install> ");
            builder.AppendLine("    <InstallFrom>Disk</InstallFrom> ");
            builder.AppendLine("    <UpdateEnabled>false</UpdateEnabled> ");
            builder.AppendLine("    <UpdateMode>Foreground</UpdateMode> ");
            builder.AppendLine("    <UpdateInterval>7</UpdateInterval> ");
            builder.AppendLine("    <UpdateIntervalUnits>Days</UpdateIntervalUnits> ");
            builder.AppendLine("    <UpdatePeriodically>false</UpdatePeriodically> ");
            builder.AppendLine("    <UpdateRequired>false</UpdateRequired> ");
            builder.AppendLine("    <MapFileExtensions>true</MapFileExtensions> ");
            builder.AppendLine("    <ApplicationRevision>0</ApplicationRevision> ");
            builder.AppendLine("    <ApplicationVersion>1.0.0.%2a</ApplicationVersion> ");
            builder.AppendLine("    <IsWebBootstrapper>false</IsWebBootstrapper> ");
            builder.AppendLine("    <UseApplicationTrust>false</UseApplicationTrust> ");
            builder.AppendLine("    <BootstrapperEnabled>true</BootstrapperEnabled> ");
            builder.AppendLine("    <TargetFrameworkProfile /> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' \"> ");
            builder.AppendLine("    <OutputPath>bin\\Debug\\</OutputPath> ");
            builder.AppendLine("    <AllowUnsafeBlocks>false</AllowUnsafeBlocks> ");
            builder.AppendLine("    <BaseAddress>285212672</BaseAddress> ");
            builder.AppendLine("    <CheckForOverflowUnderflow>false</CheckForOverflowUnderflow> ");
            builder.AppendLine("    <ConfigurationOverrideFile> ");
            builder.AppendLine("    </ConfigurationOverrideFile> ");
            builder.AppendLine("    <DefineConstants>DEBUG;TRACE</DefineConstants> ");
            builder.AppendLine("    <DocumentationFile> ");
            builder.AppendLine("    </DocumentationFile> ");
            builder.AppendLine("    <DebugSymbols>true</DebugSymbols> ");
            builder.AppendLine("    <FileAlignment>4096</FileAlignment> ");
            builder.AppendLine("    <NoStdLib>false</NoStdLib> ");
            builder.AppendLine("    <NoWarn> ");
            builder.AppendLine("    </NoWarn> ");
            builder.AppendLine("    <Optimize>false</Optimize> ");
            builder.AppendLine("    <RegisterForComInterop>false</RegisterForComInterop> ");
            builder.AppendLine("    <RemoveIntegerChecks>false</RemoveIntegerChecks> ");
            builder.AppendLine("    <TreatWarningsAsErrors>false</TreatWarningsAsErrors> ");
            builder.AppendLine("    <WarningLevel>4</WarningLevel> ");
            builder.AppendLine("    <DebugType>full</DebugType> ");
            builder.AppendLine("    <ErrorReport>prompt</ErrorReport> ");
            builder.AppendLine("    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' \"> ");
            builder.AppendLine("    <OutputPath>bin\\Release\\</OutputPath> ");
            builder.AppendLine("    <AllowUnsafeBlocks>false</AllowUnsafeBlocks> ");
            builder.AppendLine("    <BaseAddress>285212672</BaseAddress> ");
            builder.AppendLine("    <CheckForOverflowUnderflow>false</CheckForOverflowUnderflow> ");
            builder.AppendLine("    <ConfigurationOverrideFile> ");
            builder.AppendLine("    </ConfigurationOverrideFile> ");
            builder.AppendLine("    <DefineConstants>TRACE</DefineConstants> ");
            builder.AppendLine("    <DocumentationFile> ");
            builder.AppendLine("    </DocumentationFile> ");
            builder.AppendLine("    <DebugSymbols>false</DebugSymbols> ");
            builder.AppendLine("    <FileAlignment>4096</FileAlignment> ");
            builder.AppendLine("    <NoStdLib>false</NoStdLib> ");
            builder.AppendLine("    <NoWarn> ");
            builder.AppendLine("    </NoWarn> ");
            builder.AppendLine("    <Optimize>true</Optimize> ");
            builder.AppendLine("    <RegisterForComInterop>false</RegisterForComInterop> ");
            builder.AppendLine("    <RemoveIntegerChecks>false</RemoveIntegerChecks> ");
            builder.AppendLine("    <TreatWarningsAsErrors>false</TreatWarningsAsErrors> ");
            builder.AppendLine("    <WarningLevel>4</WarningLevel> ");
            builder.AppendLine("    <DebugType>none</DebugType> ");
            builder.AppendLine("    <ErrorReport>prompt</ErrorReport> ");
            builder.AppendLine("    <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <Reference Include=\"LCL.Common\"> ");
            builder.AppendLine("      <HintPath>..\\Lib\\LCL.Common.dll</HintPath> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System\"> ");
            builder.AppendLine("      <Name>System</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Data\"> ");
            builder.AppendLine("      <Name>System.Data</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Drawing\"> ");
            builder.AppendLine("      <Name>System.Drawing</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.Xml\"> ");
            builder.AppendLine("      <Name>System.XML</Name> ");
            builder.AppendLine("    </Reference> ");
            builder.AppendLine("    <Reference Include=\"System.configuration\" /> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <Compile Include=\"DataAccess.cs\" /> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <ItemGroup> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Net.Client.3.5\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName> ");
            builder.AppendLine("      <Install>false</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Net.Framework.3.5.SP1\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>.NET Framework 3.5 SP1</ProductName> ");
            builder.AppendLine("      <Install>true</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("    <BootstrapperPackage Include=\"Microsoft.Windows.Installer.3.1\"> ");
            builder.AppendLine("      <Visible>False</Visible> ");
            builder.AppendLine("      <ProductName>Windows Installer 3.1</ProductName> ");
            builder.AppendLine("      <Install>true</Install> ");
            builder.AppendLine("    </BootstrapperPackage> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("<ItemGroup> ");
            builder.AppendLine("    <ProjectReference Include=\"..\\IDAL\\" + NameSpace + ".IDAL.csproj\"> ");
            builder.AppendLine("      <Project>{" + IDALGuid + "}</Project> ");
            builder.AppendLine("      <Name>" + NameSpace + ".IDAL</Name> ");
            builder.AppendLine("    </ProjectReference> ");
            builder.AppendLine("  </ItemGroup> ");
            builder.AppendLine("  <Import Project=\"$(MSBuildBinPath)\\Microsoft.CSharp.targets\" /> ");
            builder.AppendLine("  <PropertyGroup> ");
            builder.AppendLine("    <PreBuildEvent> ");
            builder.AppendLine("    </PreBuildEvent> ");
            builder.AppendLine("    <PostBuildEvent> ");
            builder.AppendLine("    </PostBuildEvent> ");
            builder.AppendLine("  </PropertyGroup> ");
            builder.AppendLine("</Project> ");
            string filename = CodeCommon.DesktopDirectory + NameSpace + @"/DALFactory/" + NameSpace + ".DALFactory.csproj";
            CodeCommon.CreateFiles(filename, builder.ToString());
        }
        #endregion
    }
}
